//
//  ViewController.m
//  动态创建对象
//
//  Created by MyMac on 15/8/26.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "ViewController.h"
#import <objc/runtime.h>
//#import "NSObject+exchangeMethon.h"
#import "NSMutableArray+exchange.h"
@interface ViewController ()
@property(nonatomic,copy)NSString*titleString;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray*array=[[NSMutableArray alloc]init];
    NSLog(@"%@",array.class);
    [array addObject:@"xinzeng"];
    //[array exchangeAdd:@"333333"];
  
    
    
//    unsigned int count=0;
//  objc_property_t *list=  class_copyPropertyList([self class], &count);
//    for (int i=0; i<count; i++) {
//        objc_property_t pro=list[i];
//        if(property_getName(pro)==[@"titleString" cStringUsingEncoding:NSASCIIStringEncoding]){
//            const char*att=property_getAttributes(pro);
//            NSLog(@"%s",att);
//        }
//    }
//    
//   [array logAddObject:@"111111"];
//    [array loadNewAddObject:@"22222"];
//  
    
    
    
    
    
    NSLog(@"%@",array);
    self.view.backgroundColor=[UIColor whiteColor];

}
 static NSMutableDictionary*dict;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSString*class=@"qFirstViewController";
    const char*className=[class cStringUsingEncoding:NSASCIIStringEncoding];
    //得到一个类，，，当这个类在工程中有实名文件时可以返回成功，，如果没有实名文件则返回失败
    Class newClass=  objc_getClass(className);
    NSLog(@"%@ %s",newClass,@encode(typeof(void)));
    //动态创建一个类，无实名文件
    if(!newClass){
        //父类
        Class supClass=[UIViewController class];
        //新类
        newClass=objc_allocateClassPair(supClass, className, 0);
        //增加方法
        class_addMethod(newClass, @selector(playerClick:), imp_implementationWithBlock(^(id self,SEL _cmd,NSString*name){
        
            NSLog(@"%@",name);
            
        
        }), method_getTypeEncoding(class_getInstanceMethod([self class], @selector(playerClick:))));//查找参数的编码
        //增加属性

#warning This function may only be called after objc_allocateClassPair and before objc_registerClassPair.只能在这两个方法之间定义
        //增加成员变量
        if(    class_addIvar(newClass, "IvarName", sizeof(NSString*), 0, "@")){
        
            NSLog(@"增加成员变量成功");
        
        
        }
        //T@"NSString",C,N,V_titleString
      //增加属性
        objc_property_attribute_t type = { "T", "@\"NSString\"" };
        objc_property_attribute_t ownership = {"C",""}; // C = copy
        objc_property_attribute_t backingivar  = { "V", "_privateName" };
        objc_property_attribute_t attrs[] = { type, ownership, backingivar };
        
        if(class_addProperty(newClass, "name", attrs, 3)){
            objc_property_t po=class_getProperty(newClass, "name");
          const char*at= property_getAttributes(po);
            NSLog(@"增加属性成功%s",at);
            unsigned int pCount=0;
            objc_property_t*list=class_copyPropertyList(newClass, &pCount);
            for (int i=0; i<pCount; i++) {
                objc_property_t property=list[i];
                NSLog(@"%s",property_getName(property));
            }
           
            dict=[NSMutableDictionary dictionary];
            
            Method setM=class_getInstanceMethod(newClass, @selector(setName:));
            Method getM=class_getInstanceMethod(newClass, @selector(name));
       //动态添加属性，必须重写set和get方法
            if(!setM){
        BOOL isSet= class_addMethod(newClass, @selector(setName:),(IMP)setIMP,"v@:@");
            }
            if(!getM){
        BOOL isGet=  class_addMethod(newClass, @selector(name), (IMP)getIMP,"@@:");
            
        };
        }
        
        
        
        //注册新类
        objc_registerClassPair(newClass);
  
     
     
    
    
    
    }
    //实例化类

    id instance=[[newClass alloc]init];
 
    //当不知道对象的具体类型，无法用点语法时，，可以用KVC赋值
    [instance setValue:[UIColor yellowColor] forKeyPath:@"view.backgroundColor"];
    //成员变量赋值
    [instance setValue:@"新成员变量" forKey:@"IvarName"];
    //属性赋值
    [instance setValue:@"新属性" forKey:@"name"];
 
    [instance performSelector:@selector(playerClick:) withObject:@"li  si"];
   //objc_msgSend(instance, @selector(playerClick:), @"li si");
    NSLog(@"%@  %@   %@",newClass,[instance valueForKey:@"IvarName"],[instance valueForKey:@"name"]);
    [self presentViewController:instance animated:YES completion:nil];

    
}
//set实现
static void setIMP(id self,SEL _cmd,id value){


    [dict setObject:value forKey:@"name"];


}
//get实现
static NSString* getIMP(id self,SEL _cmd){

    return dict[@"name"];

}
-(void)playerClick:(NSString*)name
{

    NSLog(@"%@",name);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
